﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class cargoController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /cargo/

        public ActionResult Index()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string cargo = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.CARGOes
                .Where(a => a.IdCargo >= 1);
            if (!String.IsNullOrEmpty(cargo))
            {
                query = query.Where(a => (a.NomCargo).ToUpper().Contains(cargo.ToUpper()));
            }
            var result = (from a in query.OrderBy(a => a.NomCargo).Skip(start).Take(limit).ToList()
                          select new
                          {
                              IdCargo = a.IdCargo,
                              NomCargo = a.NomCargo,
                              DescCargo = a.DescCargo
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /cargo/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /cargo/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /cargo/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, CARGO cargo)
        {

            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {

                    var crg = new CARGO();
                    TryUpdateModel(crg, collection.ToValueProvider());
                    entidades.AddToCARGOes(crg);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        
        //
        // GET: /cargo/Edit/5
 
        //
        // POST: /cargo/Edit/5

        public ActionResult Edit(int id)
        {
            var ti = entidades.CARGOes.Single(tia => tia.IdCargo == id);
            return View(ti);
        }

        //
        // POST: /tipoafiliacion/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, CARGO CARGO)
        {
            var ta = entidades.CARGOes.Single(tia => tia.IdCargo == id);
            try
            {

                if (ModelState.IsValid)
                {

                    TryUpdateModel(ta);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View(ta);
            }
            catch
            {
                return View(ta);
            }
        }

        //
        // GET: /cargo/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /cargo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
